package com.aiyige.page.player.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.base.BaseActivity;
import com.aiyige.page.player.PlayerController;
import com.aiyige.page.player.PlayerInfo;
import com.aiyige.utils.UIHelper;
import com.chad.library.adapter.base.BaseQuickAdapter;


public class PlayListMenuView extends LinearLayout {
    Context mContext;
    View mView;
    RecyclerView recyclerView;
    PlayListMenuAdapter adapter;
    TextView tvVideoCount;

    public PlayerController getPlayerController() {
        return playerController;
    }

    public void setPlayerController(PlayerController playerController) {
        this.playerController = playerController;
    }

    PlayerController playerController;

    public PlayListMenuView(Context context, PlayerController playerController) {
        super(context);
        this.mContext = context;
        this.playerController = playerController;
        init();
    }


    public PlayListMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    private void init() {

        this.mView = LayoutInflater.from(mContext).inflate(R.layout.dialog_player_menu, this);

        recyclerView = (RecyclerView)mView.findViewById(R.id.rv_player_menu);

        tvVideoCount = (TextView)mView.findViewById(R.id.tv_video_count);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)this.getLayoutParams();
        lp.width = UIHelper.dip2px(150);
        lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
        setLayoutParams(lp);
        // 设置弹出窗体可点击
        this.setFocusable(true);

        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
//        // 设置弹出窗体的背景
        this.setBackgroundDrawable(dw);

        // 设置弹出窗体显示时的动画，从底部向上弹出
//        this.setAnimationStyle(R.style.player_selection_dialog_anim);
//        this.setOutsideTouchable(true);
        adapter = new PlayListMenuAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);
//        recyclerView.addItemDecoration(new MyRecyclerViewDivider(
//                mContext, LinearLayoutManager.VERTICAL, R.color.white));
        //添加自定义分割线
        DividerItemDecoration divider = new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(mContext,R.drawable.play_list_line));
        recyclerView.addItemDecoration(divider);
        adapter.setCurrentPlayInfo(playerController.getiPlayerView().getCurrentPlayInfo());
        adapter.addData(playerController.getiPlayerView().getPlayList());
        tvVideoCount.setText(mContext.getString(R.string.video_count_value,String.valueOf(playerController.getiPlayerView().getPlayList().size())));
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                playerController.onSwitch(position);
                dismiss();
            }
        });
    }

    public void setCurrentPlayInfo(PlayerInfo playInfoy) {
        if (adapter != null) {
            adapter.setCurrentPlayInfo(playInfoy);
        }
    }

    public void notifyDataChanged() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    public void show() {
        init();
        setVisibility(View.VISIBLE);
        playerController.hideWidget();
//        int height = playerController.getLayoutParams().height;
//        showAtLocation(playerController, Gravity.RIGHT,0,height);
    }
//    @Override
    public void dismiss() {
//        super.dismiss();
//        playerController.showWidget();
//        UIHelper.hideSystemUI((BaseActivity)mContext);
        playerController.showWidget();
        UIHelper.hideSystemUI((BaseActivity)mContext);
        setVisibility(View.GONE);
    }
}
